#!/usr/bin/env python
import random,string,hashlib,argparse

'''
Description:
this script generate password with chosen hash type
hashtype available: md5, sha1, sha224, sha256, sha384, and sha512 (depend on hashlib module lah)

Python Version:
2.7

Author:
weldan jamili, sept 26, 2011
mweldan@gmail.com , http://mweldan.net

'''

def generate_passwd(hashtype, key_string, salt):
        
        if (hashtype == "md5"):
                password = hashlib.md5( salt + key_string ).hexdigest()
        if (hashtype == "sha1"):
                password = hashlib.sha1( salt + key_string ).hexdigest()
        if (hashtype == "sha224"):
                password = hashlib.sha224( salt + key_string ).hexdigest()
        if (hashtype == "sha256"):
                password = hashlib.sha256( salt + key_string ).hexdigest()
        if (hashtype == "sha384"):
                password = hashlib.sha384( salt + key_string ).hexdigest()
        if (hashtype == "sha512"):
                password = hashlib.sha512( salt + key_string ).hexdigest()
        
        return [hashtype, salt, key_string, password]
        

parser = argparse.ArgumentParser(description="Generate password", conflict_handler="resolve")
parser.add_argument('-t', 
        action='store', 
        dest='hashtype', 
        help='set hash type'
)
parser.add_argument('-k', 
        action='store', 
        dest='key_string', 
        help='set key string'
)
parser.add_argument('-s', 
        action='store', 
        dest='salt', 
        help='set salt'
)

args = parser.parse_args()

if (args.hashtype != None and args.key_string != None and args.salt != None ):
        result = generate_passwd(args.hashtype, args.key_string, args.salt)
        print "Hash: "+result[0]
        print "Salt: "+result[1]
        print "Cleartext Password: "+result[2]
        print "Hashed Password: "+result[3]
else:
        print parser.print_help()
